package com.element.security.model.entity;

import com.element.security.model.ScopeGrantedAuthority;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @auther WuJun.Zhang
 * @date 2022/2/18 下午9:11
 */
@Data
public class AuthClientE implements ClientDetails {

    private String clientId;

    private String clientSecret;

    private Set<String> registeredRedirectUri;

    private Set<String> authorizedGrantTypes;

    private boolean isScoped;

    private boolean isSecretRequired;

    private String autoApprove;

    private Integer accessTokenValiditySeconds;

    private Integer refreshTokenValiditySeconds;

    private Set<String> resourceIds;

    private Map<String, Set<String>> scopeApis;

    private Map<String, Object> additionalInformation;

    @Override
    public boolean isSecretRequired() {
        return this.isSecretRequired;
    }

    @Override
    public boolean isScoped() {
        return this.isScoped;
    }

    @Override
    public Set<String> getScope() {
        if (CollectionUtils.isEmpty(scopeApis)) {
            return null;
        }
        return scopeApis.keySet();
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        if (CollectionUtils.isEmpty(scopeApis)) {
            return new ArrayList<>();
        }
        List<GrantedAuthority> authorityList = new ArrayList<>(scopeApis.size());
        scopeApis.forEach((key, value) -> authorityList.add(new ScopeGrantedAuthority(key, value)));
        return authorityList;
    }

    @Override
    public boolean isAutoApprove(String autoApprove) {
        if (null == autoApprove || "".equals(autoApprove)) {
            return false;
        }
        return "true".equals(autoApprove);
    }
}